package com.cn.JUC.utils.case1;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Title 问题
 * @Description Account存在什么问题?
 * @Author CAI ZHAI
 * @Date 2021/9/1 15:22
 **/
public class zQuestion{
    
    class Account{
        private int balance;
        private final Lock lock
                = new ReentrantLock();
    
        // 转账
        void transfer(Account tar,int amt){
            while(true){
                if(this.lock.tryLock()){
                    try{
                        if(tar.lock.tryLock()){
                            try{
                                this.balance -= amt;
                                tar.balance += amt;
                            }finally{
                                tar.lock.unlock();
                            }
                        }
                    }finally{
                        this.lock.unlock();
                    }
                }
            }
        }
    }
    
    //问题1：会出现死锁吗？  不会，会出现活锁
    //问题2：while(true)的问题，代码正常执行完，没用break，会一直死循环
}